package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.mapper.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Package;
import com.woniu.service.IFrontUserService;
import com.woniu.vo.ServiceSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class FrontUserServiceImpl implements IFrontUserService {
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private TeamOrganizationMapper teamOrganizationMapper;
    @Resource
    private TeamPackageMapper teamPackageMapper;
    @Resource
    private PackageMapper packageMapper;
    @Resource
    private TeamDoctorMapper teamDoctorMapper;
    @Resource
    private SignMapper signMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ServicesMapper servicesMapper;
    /**
     * 根据机构id查询所有团队
     * @param organizationId
     * @return
     */

    public List<Team> getAllTeamByOrganizationId(Long organizationId) {
        QueryWrapper<TeamOrganization> teamOrganizationQueryWrapper = new QueryWrapper<>();
        teamOrganizationQueryWrapper.eq("organization_id",organizationId);
        List<TeamOrganization> teamOrganizations = teamOrganizationMapper.selectList(teamOrganizationQueryWrapper);
        List<Team> teamList=new ArrayList<>();
        for (TeamOrganization teamOrganization : teamOrganizations) {
            Long teamId = teamOrganization.getTeamId();
            Team team = teamMapper.selectById(teamId);
            teamList.add(team);
        }
        return teamList;
    }

    /**
     * 根据机构id查询所有服务包的信息
     * @param organizationId
     * @return
     */
    public List<Package> getAllPackageByOrganizationId(Long organizationId) {
        //查询到所有团队
        List<Team> allTeamByOrganizationId = getAllTeamByOrganizationId(organizationId);
        List<Package> packageList=new ArrayList<>();
        for (Team team : allTeamByOrganizationId) {
            Long teamId = team.getId();
            QueryWrapper<TeamPackage>  teamPackageQueryWrapper= new QueryWrapper<>();
            teamPackageQueryWrapper.eq("team_id",teamId);
            //查询该机构下所有团队的服务包
            List<TeamPackage> teamPackages = teamPackageMapper.selectList(teamPackageQueryWrapper);
            for (TeamPackage teamPackage : teamPackages) {
                Long packageId = teamPackage.getPackageId();
                Package aPackage = packageMapper.selectById(packageId);
                packageList.add(aPackage);
            }
        }
        return packageList;
    }

    /**
     * 根据团队id查询所有医生
     * @param teamId
     * @return
     */
    public List<Doctor> getAllDoctorByTeamId(Long teamId) {
        QueryWrapper<TeamDoctor> teamDoctorQueryWrapper = new QueryWrapper<>();
        teamDoctorQueryWrapper.eq("team_id",teamId);
        List<Doctor> doctorList=new ArrayList<>();
        List<TeamDoctor> teamDoctors = teamDoctorMapper.selectList(teamDoctorQueryWrapper);
        for (TeamDoctor teamDoctor : teamDoctors) {
            Long doctorId = teamDoctor.getDoctorId();
            Doctor doctor = doctorMapper.selectById(doctorId);
            doctorList.add(doctor);
        }
        return doctorList;
    }

    /**
     * 签约，提交申请
     * @param sign
     * @return
     */
    public Boolean doSign(Sign sign) {
        //如果是重新申请 ,则把驳回的状态改为0
        String refuseRedis = stringRedisTemplate.opsForValue().get("refuse:" + sign.getId());
        if (refuseRedis!=null || sign.getId()!=null &&sign.getId()>0 ){
            sign.setStatus("0");
            sign.setSignStatusId(4L);
            //设置提交审核的时间
            Date date = new Date();
            sign.setAuditTime(date);
            int insert = signMapper.updateById(sign);
            //清除驳回的redis
            stringRedisTemplate.delete("refuse:"+sign.getId());
            //设置reids
            stringRedisTemplate.opsForValue().set("audit"+sign.getId(),"已申请");
            if(insert<0){
                return false;
            }
            return true;
        }
        //设置签约的状态为签约中
        sign.setStatus("0");
        //待审核
        sign.setSignStatusId(4L);
        //设置提交审核的时间
        Date date = new Date();
        sign.setAuditTime(date);
        //设置reids
        int insert = signMapper.insert(sign);
        stringRedisTemplate.opsForValue().set("audit"+sign.getId(),"已申请");

        if(insert<0){
            return false;
        }
        return true;
    }
    /**
     * 审核修改状态为1
     */

    /**
     * 支付
     * @param sign
     * @return
     */
    public Boolean doPay(Sign sign) {
        //改为签约成功，生效
        sign.setStatus("4");
        sign.setSignStatusId(2L);
        //设置支付时间
        Date date = new Date();
        sign.setPayTime(date);
        //清楚redis的定时
        stringRedisTemplate.delete("payTime"+sign.getId());
        int updateById = signMapper.updateById(sign);
        if (updateById<0){
            return false;
        }
        return true;
    }

    /**
     * 根据签约的id查看签约信息
     * @param signId
     * @return
     */
    public Sign querySignById(Long signId) {
        //是否申请
        String auditRedis = stringRedisTemplate.opsForValue().get("audit" + signId);
        if (auditRedis!=null){
            System.err.println(auditRedis);
            Sign sign = signMapper.selectById(signId);
            return sign;
        }
        //查询订单是否过期
        String payTimeRedis = stringRedisTemplate.opsForValue().get("payTime" + signId);
        if (payTimeRedis!=null){
            System.err.println(payTimeRedis);
            Sign sign = signMapper.selectById(signId);
            return sign;
        }
        //如果订单过期了
        Sign sign = signMapper.selectById(signId);
        sign.setStatus("5");
        return sign;
    }


    /**
     * 申请服务
     * @param services
     * @return
     */
    /**
     * 申请服务
     * @param serviceSubmitVo
     * @return
     */
    public Boolean applyService(ServiceSubmitVo serviceSubmitVo) {
        if (serviceSubmitVo.getId()>0){
            Services services = servicesMapper.selectById(serviceSubmitVo.getId());
            services.setServiceStatusId(1L);
            int i = servicesMapper.updateById(services);
            return true;
        }
        //新增一个服务
        Services services = new Services();
        BeanUtils.copyProperties(serviceSubmitVo,services);
        Date date = new Date();
        //设置提交时间
        services.setAppointmentTime(date);
        int insert = servicesMapper.insert(services);
        if (insert<0){
            return false;
        }
        return true;

    }
}
